Sometimes you will want to do one thing if the condition is true, and another if the condition is false. In this situation you can add an else
part to the program:
if ( condition ) statement1
else statement2 ;
The statement following the else
is performed if the condition is false. Note that this doesn't let you do anything which was otherwise impossible, but it does make programs easier to write. You don't need to add the else part if you have no need for it.
As with the standard if
construction, you can collect a bunch of statements up into a block in place of the single statement.
I tend to use the block construction even if I am only obeying one statement. It makes the program clearer, and it is also easier for me to add statements later if I need to.
On the right you can see a program using an else construction.
/* Using else */
void main ( void )
{
int count = 100 ;
if ( count < 99 )
{
/* get here if count is */
/* less than 99 */
}
else
{
/* get here if count is */
/* not less than 99 */
}
}